#
# @lc app=leetcode.cn id=2342 lang=python3
#
# [2342] 数位和相等数对的最大和
#

# @lc code=start
from typing import List


class Solution:
    def f(self,x:int) -> int:
        s = 0
        while x:
            s += x%10
            x //= 10
        return s
    
    def maximumSum(self, nums: List[int]) -> int:
        ans = -1 
        cnt = {}
        for i,x in enumerate(nums):
            y = self.f(x)
            if y in cnt:
                ans = max(ans,x + cnt[y])
            cnt[y] = max(cnt.get(y,0),x)  #字典的get  Return the value for key if key is in the dictionary, else default.
        return ans

        
# @lc code=end

